`
https://leetcode.cn/problems/binary-tree-maximum-path-sum/
`

/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxPathSum = function (root) {
  if (root === null) return 0

  let res = -Infinity
  const traverse = (root) => {
    if (root === null) return 0

    // 获取左右子树的总和最大值路径的总和
    const leftMaxSum = Math.max(0, traverse(root.left))
    const rightMaxSum = Math.max(0, traverse(root.right))

    // 总和最大值路径接上本结点
    const pathMaxSum = root.val + leftMaxSum + rightMaxSum
    // 更新答案
    res = Math.max(res, pathMaxSum)

    // 返回左右子树中更大的路径和
    return Math.max(leftMaxSum, rightMaxSum) + root.val
  }

  traverse(root)

  return res
};